﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 在拖拽IK的时候, 运行时记录一个物体的全部位置和旋转,结束的时候粘贴上去
    /// </summary>
    public class RecordTransform
    {
        public static RecordTransInfo memoryRecord;

        [MenuItem("Plateface/Animation/记录全部位置和旋转")]
        public static void Record()
        {
            var go = Selection.activeGameObject;
            if (go != null)
            {
                RecordTransInfo copy = new RecordTransInfo();
                copy.name = go.transform.name;
                copy.position = go.transform.position;
                copy.rotation = go.transform.rotation;
                copy.scale = go.transform.lossyScale;
                RecordRecursion(go, copy);
                Debug.Log("拷贝成功");
                memoryRecord = copy;
            }
        }

        [MenuItem("Plateface/Animation/粘贴全部位置和旋转")]
        public static void Paste()
        {
            if (memoryRecord == null){
                return;
            }
            
            var go = Selection.activeGameObject;
            if (go != null)
            {
                go.transform.position = memoryRecord.position;
                go.transform.rotation = memoryRecord.rotation;
                go.transform.localScale = memoryRecord.scale;
                PasteRecursion(go,memoryRecord);
                Debug.Log("粘贴成功");
            }
        }

        public static void PasteRecursion(GameObject go, RecordTransInfo info)
        {
            for (int i = 0; i < go.transform.childCount; i++)
            {
                var cur = go.transform.GetChild(i);
                RecordTransInfo childInfo = info.child[i];
                if (cur.name != childInfo.name)
                {
                    Debug.LogError("复制有冲突: " + cur.name + "  " + childInfo.name);
                }

                cur.position = childInfo.position;
                cur.rotation = childInfo.rotation;
                cur.localScale = childInfo.scale;
                PasteRecursion(cur.gameObject, childInfo);
            }
        }
        
        public static void RecordRecursion(GameObject go, RecordTransInfo info)
        {
            for (int i = 0; i < go.transform.childCount; i++)
            {
                var cur = go.transform.GetChild(i);
                RecordTransInfo childInfo = new RecordTransInfo();
                childInfo.name = cur.name;
                childInfo.position = cur.position;
                childInfo.rotation = cur.rotation;
                childInfo.scale = cur.localScale;
                info.child.Add(childInfo);
                RecordRecursion(cur.gameObject, childInfo);
            }
        }


        [System.Serializable]
        public class RecordTransInfo
        {
            public string name;
            public Vector3 position;
            public Quaternion rotation;
            public Vector3 scale;
            public List<RecordTransInfo> child = new List<RecordTransInfo>();
        }
    }
}